package com.lvmama.comm.sync.pojo;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;

import com.lvmama.comm.sync.pojo.SyncTemplate;
import com.lvmama.comm.sync.pojo.SyncTemplateSql;
import com.lvmama.comm.sync.utils.Constant;

public class Template extends SyncTemplate implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private List<SyncTemplateSql> sourceSqls;
	
	private List<SyncTemplateSql> targetSqls;

	@XmlElementWrapper(name = "sourceSqls")
	@XmlElement(name = "sql")
	public List<SyncTemplateSql> getSourceSqls() {
		return sourceSqls;
	}

	public void setSourceSqls(List<SyncTemplateSql> sourceSqls) {
		this.sourceSqls = sourceSqls;
	}

	@XmlElementWrapper(name = "targetSqls")
	@XmlElement(name = "sql")
	public List<SyncTemplateSql> getTargetSqls() {
		return targetSqls;
	}

	public void setTargetSqls(List<SyncTemplateSql> targetSqls) {
		this.targetSqls = targetSqls;
	}

	public static Template createInstance(SyncTemplate temp,
			List<SyncTemplateSql> sqls) {
		Template template = new Template();
		template.setDataSourceId(temp.getDataSourceId());
		template.setDescription(temp.getDescription());
		template.setStatus(temp.getStatus());
		template.setTableName(temp.getTableName());
		template.setTemplateName(temp.getTemplateName());
		template.setUserName(temp.getUserName());
		
		template.setSourceSqls(new ArrayList<SyncTemplateSql>());
		template.setTargetSqls(new ArrayList<SyncTemplateSql>());
		for (SyncTemplateSql sql : sqls) {
			if (Constant.SYNC_SQL_TYPE.SOURCE.name().equals(sql.getSqlType())) {
				template.getSourceSqls().add(sql);
			} else {
				template.getTargetSqls().add(sql);
			}
		}
		return template;
	}
}
